package cn.aj.org.controller;
import cn.aj.basic.annotation.PreAuthorize;
import cn.aj.basic.utils.AjaxResultUtil;
import cn.aj.basic.utils.PageListUtil;
import cn.aj.aop.LogAop;
import cn.aj.org.domain.Department;
import cn.aj.org.query.DepartmentQuery;
import cn.aj.org.service.impl.DepartmentServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
@Api(value = "部门的api", description = "部门的crud以及其它功能")
public class DepartmentController {

    @Autowired
    private DepartmentServiceImpl departmentService;

    /**
    * @Description: TODO
    * @Author: aj
    * @Version: 1.0
    * @Date:  2022/11/10 0010 13:41
     * 分页
    */
    @PostMapping
    @ApiOperation(value = "部门分页查询")
    @LogAop(name = "部门分页的方法")
    @PreAuthorize(name = "部门分页查询", sn = "department:list")
    public PageListUtil<Department> loadPage(@ApiParam(value="封装的每页条数以及页码",required = true) @RequestBody DepartmentQuery departmentQuery) {
        PageListUtil<Department> list = departmentService.loadPage(departmentQuery);
        return list;
    }

    /**
    * @Description: TODO
    * @Author: aj
    * @Version: 1.0
    * @Date:  2022/11/11 0011 22:48
     * 查询全部
    */
    @GetMapping
    @ApiOperation(value = "部门查询全部")
    @LogAop(name = "部门查询全部的方法")
    public List<Department> loadAll() {
        return departmentService.loadAll();
    }

    /**
    * @Description: TODO
    * @Author: aj
    * @Version: 1.0
    * @Date:  2022/11/11 0011 22:49
     * 根据id查询
    */
    @GetMapping("/{id}")
    @ApiOperation(value = "部门根据id查询")
    @LogAop(name = "部门根据id查询的方法")
    @PreAuthorize(name = "部门根据id查询", sn = "department:getById")
    public Department getById(@ApiParam(value="部门id",required = true) @PathVariable("id") Long id) {
        return departmentService.getById(id);
    }


    /**
    * @Description: TODO
    * @Author: aj
    * @Version: 1.0
    * @Date:  2022/11/10 0010 13:43
     * 删除
    */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "部门删除")
    @LogAop(name = "部门删除方法")
    public AjaxResultUtil delete(@ApiParam(value="部门id",required = true) @PathVariable("id") Long id) {
            departmentService.delete(id);
            return AjaxResultUtil.getAjaxResult().success();
    }

    /**
    * @Description: TODO
    * @Author: aj
    * @Version: 1.0
    * @Date:  2022/11/10 0010 14:06
     * 新增或者修改业务
    */
    @PutMapping
    @ApiOperation(value = "部门新增或者修改")
    public AjaxResultUtil addOrUpdate(@ApiParam(value="部门实体类",required = true) @RequestBody Department department) {
        if (department.getId() == null) { // 新增
            departmentService.add(department);
        } else { // 修改
            departmentService.update(department);
        }
        return AjaxResultUtil.getAjaxResult().success();
    }

    /**
    * @Description: TODO
    * @Author: aj
    * @Version: 1.0
    * @Date:  2022/11/12 0012 10:26
     * 批量删除
    */
    @PatchMapping
    @ApiOperation(value = "部门批量删除")
    @LogAop(name = "部门批量删除方法")
    public AjaxResultUtil batchDelete(@ApiParam(value="部门id",required = true) @RequestBody List<Long> ids) {
            departmentService.batchDelete(ids);
            return AjaxResultUtil.getAjaxResult().success();
    }

    /**
    * @Description: TODO
    * @Author: aj
    * @Version: 1.0
    * @Date:  2022/11/13 0013 23:24
     * 无限极树
    */
    @PostMapping("/tree")
    @ApiOperation(value = "部门无限极树")
    @LogAop(name = "部门无限极树")
    public List<Department> loadTree() {
        return departmentService.loadTree();
    }
}
